package de.game.actor.tower.projectile;

import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.List;

import de.core.gameplay.actor.AbstractMovingActor;
import de.game.actor.creep.AbstractCreep;

public abstract class AbstractProjectile extends AbstractMovingActor {

    private final double damage;


    protected AbstractProjectile(BufferedImage aImage, int aRotation, double aDamage, double aSpeed, int aX, int aY) {
        super(aImage, aSpeed, aX, aY);
        damage = aDamage;

        rotate(aRotation);
    }

    public double getDamage() {
        return damage;
    }

    @Override
    public void updateGameState() {
        if (!isLocationWithinLevelBounds(getLocation()) || !isLocationWithinLevelBounds(getNextLocation(1.0f))) {
            removeFromLevel();
            return;
        }
        super.updateGameState();
    }

    @Override
    protected void act() {
        if (getLocation().getX() >= getLevel().getWidth() || getLocation().getX() <= 0
            || getLocation().getY() >= getLevel().getHeight() || getLocation().getY() <= 0) {
            removeFromLevel();
            return;
        }

        List<AbstractCreep> actors = getLevel().getActorsIntercectingWith(this, AbstractCreep.class);
        if (!actors.isEmpty()) {
            actors.get(0).addDamage(getDamage());
            removeFromLevel();
            return;
        }
    }

    private boolean isLocationWithinLevelBounds(Point2D aPoint) {
        return aPoint.getX() >= 0 && aPoint.getX() <= getLevel().getWidth()
            && aPoint.getY() >= 0 && aPoint.getY() <= getLevel().getHeight();
    }
}
